#include <cairo.h>
#include <gtk/gtk.h>

double coordx[100];
double coordy[100];

int count = 0;


/*
 * 函数名称 : on_expose_event
 * 函数介绍 : 在窗口需要刷新时调用
 */
static gboolean on_expose_event(GtkWidget *widget,
                GdkEventExpose *event,
                gpointer data)
{
        cairo_t *cr;
        
        //从window中创建一个cairo_t
        cr = gdk_cairo_create(widget->window);
        

        //设置 画笔的颜色和宽度, (蓝色,宽1)
        cairo_set_source_rgb(cr, 0, 0, 255);
        cairo_set_line_width (cr, 1);
        

        //每两个点之间画一条直线
        int i, j;
        for ( i =0; i <= count -1; i++){
                for ( j  = 0; j <= count -1; j++ ) {
                        //用于设置线段起点
                        cairo_move_to(cr, coordx[i], coordy[i]);  
                        //用于设置线段终点
                        cairo_line_to(cr, coordx[j], coordy[j]);
                }
        }
        
        count = 0;
        //函数会将 cr 中的路径绘制出来
        cairo_stroke(cr);
        cairo_destroy(cr);
        
        return FALSE;
}

gboolean clicked(GtkWidget *widget, GdkEventButton *event,
                 gpointer user_data)
{
        if (event->button ==1){
                coordx[count] = event->x;
                coordy[count++] = event->y;
        }
        
        if (event->button ==3){
                //当鼠标右键单击时，调用 gtk_widget_queue_draw () 函数重绘窗口区域。
                gtk_widget_queue_draw(widget);
        }
        
        return TRUE;
}


int
main (int argc,char *argv[])
{
        GtkWidget *window;
        
        gtk_init(&argc, &argv);
        
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        
        gtk_widget_add_events (window, GDK_BUTTON_PRESS_MASK);
        
        g_signal_connect(window, "expose-event",
                         G_CALLBACK(on_expose_event),NULL);
        g_signal_connect(window, "destroy",
                         G_CALLBACK(gtk_main_quit),NULL);
        //设定 button-press-event 事件的回调函数为 clicked ()
        g_signal_connect(window, "button-press-event", 
                         G_CALLBACK(clicked),NULL);
        
        gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
        gtk_window_set_title(GTK_WINDOW(window),"lines");
        gtk_window_set_default_size(GTK_WINDOW(window),400,300);
        gtk_widget_set_app_paintable(window, TRUE);
        
        gtk_widget_show_all(window);
        
        gtk_main();
        
        return 0;
}